package com.rs.adminweb.commons.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jaxplat.JaxplatBean;
import com.jaxplat.commons.Row;
import com.rs.adminweb.admin.delegate.AdminDelegate;

public class AccessServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{
    static final long serialVersionUID = 1L;
    private Log log = LogFactory.getLog(this.getClass());

    private AdminDelegate adminDelegate;

    public AdminDelegate getAdminDelegate()
    {
        return adminDelegate;
    }

    public void setAdminDelegate(AdminDelegate adminDelegate)
    {
        this.adminDelegate = adminDelegate;
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public AccessServlet()
    {
        super();
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
     * HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // TODO Auto-generated method stub
        // System.out.println("HELLO WORLD....");
        try
        {

            StringBuffer strBuf = new StringBuffer();
            Reader reader = request.getReader();
            char[] buffer = new char[1024];
            int i = 0;
            String strtmp = null;
            while ((i = reader.read(buffer, 0, 1024)) != -1)
            {
                strtmp = new String(buffer, 0, i);
                strBuf.append(strtmp);
            }
            System.out.println("ip:" + request.getRemoteAddr());

            System.out.println("报文:\n" + strBuf.toString());

            // 格式123456
            String cert = request.getParameter("cert");
            // 格式2012-09-05 19:30:00.0
            String date = request.getParameter("date");
            date = date.substring(0, 10);
            response.setCharacterEncoding("UTF-8");

            log.info("预约验证:{ cert:" + cert + ", date: " + date + "}");
            String returnStr = "";
            returnStr = accessCert(cert, date) + "";
            response.setCharacterEncoding("UTF-8"); 
            OutputStream out = response.getOutputStream();
            if (returnStr != null)
            {
                log.info("预约验证返回长度:" + returnStr.length());
                System.out.println("发送长度:" + returnStr.length());
                out.write(returnStr.getBytes());
                out.flush();
            }
            out.close();
        }
        catch (RuntimeException e)
        {
            e.printStackTrace();
        }
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
     * HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }

    private String accessCert(String cert, String date)
    {
        String data = "1";
        try
        {
            this.adminDelegate = (AdminDelegate) JaxplatBean.getBean("adminDelegate");
            // this.getmPayDelegate().addMPayRequest(payRequest);
            Row row = this.getAdminDelegate().access(cert, date);
            if (null != row)
            {
                data = row.getString("card_no") + "^" + row.getString("doctor_name") + "^" + row.getString("section_name") + "^"
                        + row.getString("pro_title") + "^";
                // System.out.println(+row.getString("card_no"));
            }
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            log.error("没有该数据");
        }
        log.info("预约验证结果: " + data);
        return data;
    }
}